//=============================================================================

#include <fstream>
#include <iomanip>
#include <cassert>
#include <ctime>
#include <sstream>
#include <cstring>

#include "log.hpp"

namespace KEG
{
	void Logger::Init( const char* appName )
	{
		ofs.open( "log.txt" );
		assert( ofs.good() );

		time_t rawtime;

		time( &rawtime );

		std::ostringstream oss;
		char * timeString = ctime( &rawtime );
		*(strchr( timeString, '\n' )) = 0;
		oss << "= " << appName << " : Log File created by Little Logger "
			<< timeString << " =";
		std::string header = oss.str();

		for(int i=0; i< header.length(); ++i)
		{
			ofs << '=';
		}
		ofs << std::endl;

		ofs << header << std::endl;

		for(int i=0; i< header.length(); ++i)
		{
			ofs << '=';
		}
		ofs << std::endl << std::endl;
	}

	void Logger::Write( const char* logString, const char* linefile, int lineNumber )
	{
		clock_t nowtime = clock();
		
		ofs
		<< "[I] " << logString << linefile << lineNumber << ") ("
		<< double(nowtime)/double(CLOCKS_PER_SEC) << ")" << std::endl;

	}

	void Logger::Warn( const char* logString, const char* linefile, int lineNumber )
	{
		clock_t nowtime = clock();
		
		ofs
		<< "[W] " << logString << linefile << lineNumber << ") ("
		<< double(nowtime)/double(CLOCKS_PER_SEC) << ")" << std::endl;

	}

	void Logger::Error( const char* errorString, const char* linefile, int lineNumber )
	{
		clock_t nowtime = clock();
		//std::ostringstream ostr;

		for(int i=0; i<80; ++i)
		{
			ofs << '*';
		}
		ofs << '\n';

		ofs
		<< "[!] " << errorString << linefile << lineNumber << ") ("
		<< double(nowtime)/double(CLOCKS_PER_SEC) << ")" << std::endl;

		for(int i=0; i<80; ++i)
		{
			ofs << '*';
		}
		ofs << std::endl;
	}

	Logger MainLog;
}
